import time
import unittest
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from time import sleep
from unit.Mysql import Mysql
class Msq(unittest.TestCase):
    def setUp(self):
        # 开始准备环境
        self.driver = webdriver.Edge()
        self.driver.get('http://10.59.9.14:7003/')
        self.driver.maximize_window()
        self.db = Mysql(host='10.59.9.14',user='root',password='yidong666',database='a3mall')
    def tearDown(self):
        sleep(10)
        self.driver.quit()
    def test_01_login(self):
        # 点击登录
        self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        sleep(2)
        # 输入账号密码
        self.driver.find_element(By.CLASS_NAME, 'regi-input').send_keys('15232008902')
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        sleep(15)
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/div[2]/form/div[5]/a').click()
        sleep(2)

        a1=self.db.chaxun('select * from mall_users where mobile=15232008902')
        print(a1)
        sleep(10)
        aaa = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        print(aaa)
        aa=self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[1]')
        print(aa)
    def test_02_creat(self):
        # 调用登录
        self.test_01_login()
        wen=self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print(wen)
        num1 = self.db.chaxun('select username from mall_users where username = 15232008902')
        print(num1)
        self.assertIn(num1[0][0],wen)
        a2=self.driver.find_element(By.CLASS_NAME,'color-black').text
        ding = a2.split()[1]
        print(ding)
        num2= self.db.chaxun('select amount from mall_users where username = 15232008902')
        print(num2)
        self.assertEqual(float(num2[0][0]),float(ding))
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/ul/li[1]/a').click()
        a3=self.driver.find_element(By.CLASS_NAME,'categories-title').text
        print(a3)
        assert a3 in '全部商品分类'
        sleep(5)
        sousuo = self.driver.find_element(By.CSS_SELECTOR,'.search-input')
        sousuo.send_keys('老太')
        sousuo.send_keys(Keys.ENTER)
        sleep(2)
        a4=self.driver.find_element(By.XPATH,'/html/body/div[4]/a[2]').text
        print(a4)
        assert a4 in '搜索结果'
        a5 = self.driver.find_element(By.CLASS_NAME,'list-title').text
        print(a5)
        assert a5 in '老太'
        self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img').click()
        a6=self.driver.find_element(By.CLASS_NAME,'input-regula')
        a6.clear()
        sleep(2)
        a6.send_keys('2')
        self.driver.find_element(By.ID,'buy-cart').click()
        self.driver.find_element(By.CLASS_NAME,'ask-btn').click()
        sleep(2)
        self.driver.find_element(By.CLASS_NAME,'immediate-pay').click()










if __name__ == '__main__':
    unittest.main()